home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / a_utils / perl / cterm.lha / cterm.c < prev    next >
C/C++ Source or Header  |  1993-08-13  |  5KB  |  246 lines

  1. #include <curses.h>
  2. #include "cursesXt.h"
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include "defs.h"
  6.  
  7. FILE *OUT ;
  8. WINDOW *windows[MAXNUMWINDOWS] ;
  9. int optX = 0, hasWin = 0 ;
  10.  
  11. static int hasOUT = 0 ;
  12. static FILE *LOG ;
  13. static int in = -1, out = -1 ;
  14. static int n, loglevel = -1 ;
  15. static char com[BUFSIZE], buf[BUFSIZE] ;
  16. static int comSize = 0, bufSize = 0, bufhead = 0, buftail = 0 ;
  17. static int numread = 0, numreads = 0 ;
  18. static char outstr[1024] ;
  19.  
  20. curStat()
  21. { int n,i,y,x ;
  22.   char s[80] ;
  23.  
  24.   sprintf(s,"reads %d, read %d, average %d",numreads,numread,numread/numreads) ;
  25.   getyx(stdscr,y,x) ;
  26.   mvinsch(0,0,'|') ;
  27.   for (i=strlen(s)-1 ; i>=0 ; i--)
  28.     { mvinsch(0,0,s[i]) ; }
  29.   move(y,x) ;
  30.   refresh() ;
  31. }
  32.  
  33. void redraw()
  34. { int i ;
  35.  
  36.   for ( i = 0 ; i < MAXNUMWINDOWS ; i++ )
  37.     if ( ( windows[i] != NULL ) && ( windows[i] != curscr ) )
  38.       { clearok(windows[i],1) ;
  39.         wrefresh(windows[i]) ;
  40.       }
  41. }
  42.  
  43. void addlogs(level,s)
  44. int level ;
  45. char *s ;
  46. { if ( level <= loglevel )
  47.     { fprintf(LOG,"%s\n",s) ;
  48.       fflush(LOG) ;
  49.     }
  50. }
  51.  
  52. void addlogsn(level,s,n)
  53. int level ;
  54. char *s ;
  55. int n ;
  56. { if ( level <= loglevel )
  57.     { fprintf(LOG,"%s %d\n",s,n) ;
  58.       fflush(LOG) ;
  59.     }
  60. }
  61.  
  62. void addlogss(level,s1,s2)
  63. int level ;
  64. char *s1, *s2 ;
  65. { if ( level <= loglevel )
  66.     { fprintf(LOG,"%s %s\n",s1,s2) ;
  67.       fflush(LOG) ;
  68.     }
  69. }
  70.  
  71. void quit()
  72. { addlogs(LOGCURS1,"quit") ;
  73.   if ( hasWin ) { addlogs(LOGCURS1,"endwin") ; endwin() ; }
  74.   if ( hasOUT ) fclose(OUT) ; else close(out) ;
  75.   addlogs(LOGCURS1,"gone") ;
  76.   exit(1) ;
  77. }
  78.  
  79. void sig_quit()
  80. { addlogs(LOGCURS1,"sig_quit") ;
  81.   quit() ;
  82. }
  83.  
  84. int incMod(x,y) int *x, y ; { *x = ((*x)+1) % y ; return(*x) ; }
  85.  
  86. int isemptyq() { return(bufSize == 0) ; }
  87.  
  88. void enqueue(c)
  89. char c ;
  90. { buf[buftail] = c ; bufSize++ ; incMod(&buftail,BUFSIZE) ; }
  91.  
  92. int dequeue()
  93. { char c ;
  94.   c = buf[bufhead] ; bufSize-- ; incMod(&bufhead,BUFSIZE) ; return(c) ;
  95. }
  96.  
  97. int getCom()
  98. { int res, c, i, found ;
  99.   char row[BUFSIZE2] ;
  100.   char mes[100] ;
  101.  
  102.   res = 1 ;
  103.   found = 0 ;
  104.   comSize = 0 ;
  105.  
  106.   do { c = 'x' ;
  107.        while ( ( !isemptyq() ) && ( (c = dequeue()) != SEP ) )
  108.      { com[comSize++] = c ; }
  109.        if (c == SEP)
  110.      { com[comSize] = 0 ;
  111.        found = 1 ;
  112.      }
  113.        else
  114.      { res = read(in,row,BUFSIZE2) ;
  115.        if (res < 0)
  116.          { addlogs(LOGCURS1,"read error") ;
  117.            quit() ;
  118.          }
  119.        if ( res )
  120.              { numread += res ;
  121.            numreads++ ;
  122.            for (i=0 ; i<res ; i++)
  123.                  { enqueue(row[i]) ; }
  124.              }
  125.        else
  126.          { addlogs(LOGCURS1,"read 0 chars") ;
  127.            found = 1 ;
  128.          }
  129.      }
  130.      }
  131.   while ( ! found ) ;
  132.  
  133.   addlogs(LOGCURS2,com) ;
  134.  
  135.   return(res) ;
  136. }
  137.  
  138. int nextInt()
  139. { int n ;
  140.   if ( ! getCom() ) quit() ;
  141.   sscanf(com,"%d",&n) ;
  142.   addlogsn(LOGCURS1,"nextInt",n) ;
  143.   return(n) ;
  144. }
  145.  
  146. int nextCom()
  147. { int n ;
  148.   if ( ! getCom() ) quit() ;
  149.   sscanf(com,"%d",&n) ;
  150.   addlogsn(LOGCURS2,"nextCom",n) ;
  151.   return(n) ;
  152. }
  153.  
  154. int nextChr()
  155. { int c = 0 ;
  156.   if ( ! getCom() ) quit() ;
  157.   addlogsn(LOGCURS1,"nextChr",com[0]) ;
  158.   return(com[0]) ;
  159. }
  160.  
  161. char *nextStr()
  162. { if ( ! getCom() ) quit() ;
  163.   addlogss(LOGCURS1,"nextStr",com) ;
  164.   return(&(com[0])) ;
  165. }
  166.  
  167. void initWindows()
  168. { int i ;
  169.   for ( i = 0 ; i < MAXNUMWINDOWS ; i++ )
  170.     { windows[i] = NULL ; }
  171. }
  172.  
  173. void unsaveWindow(i) int i ; { windows[i] = NULL ; }
  174.  
  175. int saveWindow(w)
  176. WINDOW *w ;
  177. { int i ;
  178.   for ( i = 0 ; i < MAXNUMWINDOWS && windows[i] != NULL ; i++ ) { ; }
  179.   if ( i < MAXNUMWINDOWS )
  180.    { windows[i] = w ; return(i) ; }
  181.   else
  182.     { delwin(w) ; return(-1) ; }
  183. }
  184.  
  185. main(argc,argv)
  186. int argc ;
  187. char **argv ;
  188. { int c ;
  189.   char *prog ;
  190.  
  191.   prog = argv[0] ; argv++ ; argc-- ;
  192.  
  193.   if ( argc < 2 )
  194.     { fprintf(stderr,"Usage: cterm in out [options] [logfile [level]]\n") ; 
  195.       fprintf(stderr,"cterm version 2.1, ") ; 
  196.       fprintf(stderr,"Copyright (c) 1990, Henk P. Penning.\n") ; 
  197.       exit(1) ;
  198.     } 
  199.   else
  200.     { in  = atoi(*argv) ; argc-- ; argv++ ;
  201.       out = atoi(*argv) ; argc-- ; argv++ ;
  202.     }
  203.  
  204.   while ( ( argc > 0 ) && ( argv[0][0] == '-' ) )
  205.     { switch ( argv[0][1] ) {
  206.       case 'X' : optX = 1 ;
  207.          break ;
  208.       case 0   : fprintf(stderr,"unknown option %s\n", "-") ; 
  209.              exit(1) ;
  210.          break ;
  211.       default  : fprintf(stderr,"unknown option %c\n", argv[0][1]) ; 
  212.              exit(1) ;
  213.          break ;
  214.       }
  215.       argc-- ; argv++ ;
  216.     }
  217.  
  218.   if ( argc > 0 )
  219.     { LOG = fopen(*argv,"w") ;
  220.       if ( LOG == NULL )
  221.     { fprintf(stderr,"cannot open log file %s\n", *argv) ; 
  222.       exit(1) ;
  223.     } 
  224.       argc-- ; argv++ ;
  225.       if ( argc > 0 )
  226.         { loglevel = atoi(*argv) ; }
  227.       else
  228.         { loglevel = 0 ; } 
  229.     }
  230.  
  231.   signal(SIGINT,sig_quit) ;
  232.   signal(SIGPIPE,sig_quit) ;
  233.  
  234.   OUT = fdopen (out,"w") ; hasOUT++ ;
  235.  
  236.   addlogsn(LOGCURS1,"option X",optX) ;
  237.  
  238.   init_funtab() ;
  239.  
  240.   while (1)
  241.     { n = nextCom() ;
  242.       (*(funtab[n]))() ;
  243.     }
  244.  
  245. }
  246.